package com.czwj.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.dao.AttachmentMapper;
import com.czwj.dao.AttachmentRelyMapper;
import com.czwj.dao.BudgetExecuteMapper;
import com.czwj.dao.BudgetMainMapper;
import com.czwj.dao.BudgetSubItemMapper;
import com.czwj.dao.ProjectItemMapper;
import com.czwj.entities.Attachment;
import com.czwj.entities.AttachmentRely;
import com.czwj.entities.BudgetExecute;
import com.czwj.entities.BudgetMain;
import com.czwj.entities.ProjectItem;
import com.czwj.service.BudgetExecuteService;

@Service
public class BudgetExecuteServiceImpl implements BudgetExecuteService {
	@Resource
	BudgetExecuteMapper mapper;
	@Resource
	BudgetSubItemMapper submapper;
	@Resource
	BudgetMainMapper mainmapper;
	@Resource
	ProjectItemMapper itemmapper;
	@Resource
	AttachmentMapper amapper;
	@Resource
	AttachmentRelyMapper relymapper;
	
	
	@Value("${resources.phypath}")
	private String PhyPath;

	@Value("${resources.respath}")
	private String ResPath;
	
	private final String Attachment_Budget_Execute_Path = "预算执行/";

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	@Override
	public List<BudgetExecute> searchbylevel3(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.searchbylevel3(map);
	}

	@Override
	public int countbylevel3(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.countbylevel3(map);
	}

	@Override
	public int update(BudgetExecute budgetExecute) {
		// TODO Auto-generated method stub
		return this.mapper.updateByPrimaryKeySelective(budgetExecute);
	}

	@Override
	public int insert(BudgetExecute budgetExecute) {
		// TODO Auto-generated method stub
		return this.mapper.insertSelective(budgetExecute);
	}

	@Transactional
	@Override
	public int insertorupdate(Integer id, Integer itemid, Integer projectitemid,Integer projectitemchildid, String executedate, String money,
			String content, Integer status, int createrid, String createip, MultipartFile[] images) {
		// TODO Auto-generated method stub
		int count = 0;
		if(id==null){
			try {
				BudgetExecute budgetExecute;
				if(projectitemchildid!=null){
					budgetExecute = new BudgetExecute(id, projectitemid, projectitemchildid,itemid, content, money, sdf.parse(executedate), createrid, new Date(), createip, 4);
				}else{
					budgetExecute = new BudgetExecute(id, projectitemid,itemid, content, money, sdf.parse(executedate), createrid, new Date(), createip, 4);
				}
				count += this.mapper.insertSelective(budgetExecute);
				ProjectItem item = this.itemmapper.selectByPrimaryKey(projectitemid);
				String path = item.getName()+"/"+Attachment_Budget_Execute_Path+executedate+"/";
				String sourcepath = PhyPath+path;//存储路径
				String urlpath = ResPath+path;//资源路径
				File dest = new File(sourcepath);
				if (!dest.exists()) {
					dest.mkdirs();
				}
				for (MultipartFile image : images) {
					String filename = image.getOriginalFilename();
					if(!filename.contains(".")) {
						filename+=".jpg";
					}
					String _sourcepath = sourcepath+filename;
					String _urlpath =urlpath+filename;
					File file = new File(_sourcepath);
					while (file.exists()) {
						String[] arr =  _sourcepath.split("\\.");
						_sourcepath="";
						for(int i=0;i<arr.length;i++){
							if(i<arr.length-1){
								_sourcepath += arr[i];;
							}else{
								_sourcepath+="(1)."+arr[i];
							}
						}
						file = new File(_sourcepath);
					}
					image.transferTo(new File(_sourcepath));
					Attachment attachment = new Attachment(filename, _urlpath, _sourcepath, "image", createrid, new Date(), createip);
					count+=this.amapper.insertSelective(attachment);
					AttachmentRely aRely = new AttachmentRely("budgetexecute", budgetExecute.getId(), attachment.getId());
					count+=this.relymapper.insertSelective(aRely);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return 0;
			}
		}else{
			try {
				BudgetExecute budgetExecute = new BudgetExecute(id, projectitemid, projectitemchildid, itemid, content, money, sdf.parse(executedate), createrid, new Date(), createip, 4);
				count += this.mapper.updateByPrimaryKeySelective(budgetExecute);
				ProjectItem item = this.itemmapper.selectByPrimaryKey(projectitemid);
				String path = item.getName()+"/"+Attachment_Budget_Execute_Path+executedate+"/";
				String sourcepath = PhyPath+path;//存储路径
				String urlpath = ResPath+path;//资源路径
				File dest = new File(sourcepath);
				if (!dest.exists()) {
					dest.mkdirs(); 
				}
				for (MultipartFile image : images) {
					String filename = image.getOriginalFilename();
					if(!filename.contains(".")) {
						filename+=".jpg";
					}
					String _sourcepath = sourcepath+filename;
					String _urlpath =urlpath+filename;
					File file = new File(_sourcepath);
					while (file.exists()) {
						String[] arr =  _sourcepath.split("\\.");
						_sourcepath="";
						for(int i=0;i<arr.length;i++){
							if(i<arr.length-1){
								_sourcepath += arr[i];;
							}else{
								_sourcepath+="(1)."+arr[i];
							}
						}
						file = new File(_sourcepath);
					}
					image.transferTo(new File(_sourcepath));
					Attachment attachment = new Attachment(filename, _urlpath, _sourcepath, "image", createrid, new Date(), createip);
					count+=this.amapper.insertSelective(attachment);
					AttachmentRely aRely = new AttachmentRely("budgetexecute", budgetExecute.getId(), attachment.getId());
					count+=this.relymapper.insertSelective(aRely);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return 0;
			}
			
		}
		return count;
	}

	@Override
	public BudgetExecute selectByPrimaryKey(int id) {
		// TODO Auto-generated method stub
		return this.mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<BudgetMain> searchbylevel1(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.searchbylevel1(map);
	}

	@Override
	public int countbylevel1(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.countbylevel1(map);
	}

	@Override
	public List<BudgetMain> searchbylevel2(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.searchbylevel2(map);
	}

	@Override
	public int countbylevel2(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.countbylevel2(map);
	}

	@Override
	public List<BudgetExecute> searchReviewedLevel1(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.searchReviewedLevel1(map);
	}

	@Override
	public int countReviewedLevel1(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.countReviewedLevel1(map);
	}

	@Override
	public List<BudgetExecute> searchReviewedLevel2(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.searchReviewedLevel2(map);
	}

	@Override
	public int countReviewedLevel2(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.countReviewedLevel2(map);
	}

	@Transactional
	@Override
	public int remove(int id) {
		// TODO Auto-generated method stub
		int count=0;
		BudgetExecute budgetExecute = this.mapper.selectByPrimaryKey(id);
		count+= this.mapper.deleteByPrimaryKey(id);
		for (Attachment attachment : budgetExecute.getAttachments()) {
			File file = new File(attachment.getPath());
			if(file.exists()){
				file.delete();
			}
			count+= this.amapper.deleteByPrimaryKey(attachment.getId());
			count+= this.relymapper.deleteByAttachmentid(attachment.getId());
		}
		return count;
	}

	@Override
	public int count(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.count(map);
	}
	@Override
	public List<BudgetExecute> search(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.search(map);
	}

	@Override
	public List<BudgetExecute> getexecute(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.getexecute(map);
	}

	@Override
	public double getUnreviewedExecuteMoneySituation(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.getUnreviewedExecuteMoneySituation(map);
	}

	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.BudgetExecuteService#getUnreviewedExecuteMoneySituationchild(java.util.Map)
	*/
	@Override
	public double getUnreviewedExecuteMoneySituationchild(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.getUnreviewedExecuteMoneySituationchild(map);
	}
}
